﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace CollectionAssistant.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            // This is a example which demonstrates the differences between searching text in characters and in raw byte-array.
            string keyWord = "Ishmael";

            // Searching key word in character array. 
            char[] originalCharArray = File.ReadAllText("MobyDick.txt").ToCharArray();
            char[] keyWordCharArray = keyWord.ToCharArray();

            int index = -1;
            int pos = 0;

            Console.WriteLine("Start searching key word in character array.");

            Stopwatch watch = Stopwatch.StartNew();

            do
            {
                // Using the extension method.
                index = originalCharArray.MorrisPrattSearchFirst(keyWordCharArray, pos);

                if (index >= 0)
                {
                    Console.WriteLine("Key word \"{0}\" found. Index: {1}", keyWord, index);
                    pos += (index + keyWordCharArray.Length);
                }
            } while (index >= 0 && pos < originalCharArray.Length);
             
            watch.Stop();

            Console.WriteLine("Elapsed ticks: {0}", watch.ElapsedTicks); 
            Console.ReadKey(true);

            pos = 0; 
             
            Console.WriteLine("Start searching key word in byte array.");

            // Searching key word in byte array.
            byte[] originalByteArray = File.ReadAllBytes("MobyDick.txt");
            byte[] keyWordByteArray = Encoding.Default.GetBytes(keyWord);

            watch.Restart();

            do
            {
                // Using the extension method.
                index = originalByteArray.MorrisPrattSearchFirst(keyWordByteArray, pos);

                if (index >= 0)
                {
                    Console.WriteLine("Key word \"{0}\" found. Index: {1}", keyWord, index);
                    pos += (index + keyWordByteArray.Length);
                }
            } while (index >= 0 && pos < originalByteArray.Length);

            watch.Stop();

            Console.WriteLine("Elapsed ticks: {0}", watch.ElapsedTicks); 
            Console.ReadKey(true);
        }
    }
}
